//
//  PlayerSprite.m
//  GameProject
//
//  Created by Blair Trusler on 17/10/2011.
//  Copyright (c) 2011 Blair Trusler. All rights reserved.
//

#import "GameHeader.h"

@implementation PlayerSprite

@synthesize walkDownAnim;
@synthesize walkUpAnim;
@synthesize walkLeftAnim;
@synthesize walkRightAnim;

@synthesize joystick;
@synthesize attackButton;
@synthesize attackButton1;


-(void) dealloc{
    [walkUpAnim release];
    [walkDownAnim release];

    [walkLeftAnim release];
    [walkRightAnim release];

    [super dealloc];
}

-(void)applyJoystick:(SneakyJoystick *)aJoystick forTimeDelta:(float)deltaTime{
//    CGPoint scaledVelocity = ccpMult(aJoystick.velocity, 150.0f);
//    CGPoint oldPosition = [self position];
//    CGPoint newPosition = ccp(oldPosition.x + scaledVelocity.x * deltaTime, oldPosition.y);    
//        
    CGPoint scaledVelocity = ccpMult(aJoystick.velocity, (150.0f+speedBoost));
    
    
    oldPosition = ccp(playerX.position.x, playerX.position.y);
    // newPosition = ccp(self.position.x + scaledVelocity.x * deltaTime, self.position.y + scaledVelocity.y * /* + */ deltaTime);
    newPosition = ccp(playerX.position.x + scaledVelocity.x * deltaTime, playerX.position.y + scaledVelocity.y *  deltaTime);

    [self setPosition:newPosition];   
    
}


-(void)changeState:(CharacterStates)newState {
    [self stopAllActions];
    id action = nil;
    [self setCharacterState:newState];
    switch (newState) {
            //irrelevant:
        case kStateEnemyChase:
            break;
        case kStateEnemyPatrol:
            break;
            
        case kStateWonBattle:
            CCLOG(@"Player won the battle!");
            break;
            
        case kStateDead:
            CCLOG(@"TO DO");
            break;
            
        case kStateWalkingDown:
            action = [CCAnimate actionWithAnimation:walkDownAnim restoreOriginalFrame:NO];
            break;
        case kStateWalkingUp:
            action = [CCAnimate actionWithAnimation:walkUpAnim restoreOriginalFrame:NO];
            break;  
        case kStateWalkingLeft:
            action = [CCAnimate actionWithAnimation:walkLeftAnim restoreOriginalFrame:NO];
            break;  
        case kStateWalkingRight:
            action = [CCAnimate actionWithAnimation:walkRightAnim restoreOriginalFrame:NO];
            break;  
        case kStateIdle:
        //[self setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"Down1.png"]]; 
            if (left == TRUE){
                [self setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"Left1.png"]]; 
            }
            if (right == TRUE){
                [self setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"Right1.png"]]; 
            }
            if (up == TRUE){
                [self setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"Up1.png"]]; 

            }
            if (down == TRUE){
                [self setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"Down1.png"]]; 

            }
            break;
        case kStateCollision:
            CCLOG(@"kStateCOLLISION");
            playerX.position = oldPosition;
            //[self stopAllActions];
            //isCollision=FALSE;
            break;
    }    
    if (action != nil) {
        [self runAction:action];
    
    }

}


-(void)updateStateWithDeltaTime:(ccTime)deltaTime andListOfGameObjects:(CCArray*)listOfGameObjects {
    
        
    CGRect boundingBox = [self adjustedBoundingBox];
    
    for (GameCharacter *sprite in listOfGameObjects) {
        if ([sprite tag] == kPlayerSpriteTagValue) 
            continue;
        
        for (GameCharacter *character in listOfGameObjects) {

        CGRect characterBox = [sprite adjustedBoundingBox];
        if (CGRectIntersectsRect(boundingBox, characterBox)) {
            if ([character gameObjectType] == kEnemyType1) {
                CCLOG(@"Enemy Caught You! Game Over!");      
                [[SceneManager sharedSceneManager] runSceneWithID:kGameOverScene];     //Objective reached, load "level complete" scene
            }
            
            
//            CCLOG(@"ENEMY SPRITE COLLISION - BATTLE");
//            CCScene *loadBattleScene = [BattleScene node];
//            //some kind of sound & transition
//            [[CCDirector sharedDirector] pushScene: loadBattleScene]; //'pushes' stat scene ontop of game scene
//
//            //TODO : pickups etc
//            
//            if ([sprite tag] == kEnemyType1)
//                CCLOG(@"ENEMY CONTACT");
            
        }
//        if ([sprite tag] == kEnemyType1) {
//            CGRect characterBox = [sprite adjustedBoundingBox];
//            if (CGRectIntersectsRect(boundingBox, characterBox)) {
//                CCLOG(@"INTEERSETTTTTT");
//
//            }
//
//        }
            
        }

    }
    

    if ((self.characterState == kStateIdle) || (self.characterState == kStateWalkingDown) || (self.characterState == kStateWalkingUp) || (self.characterState == kStateWalkingLeft) || (self.characterState == kStateWalkingRight) || (self.characterState == kStateCollision)){
        
        if ((joystick.velocity.x == 0.0f) && (joystick.velocity.y == 0.0f)&&(isMoving==FALSE)) {
                [self changeState:kStateIdle];
        } 
        
        if (isCollision==TRUE){
            [self changeState:kStateCollision];
        }
        
        if (kStateCollision == TRUE){
            [self changeState:kStateCollision];
        }
        if (self.characterState == kStateCollision){
            [self changeState:kStateCollision];
            return;
        }
        
        //Accelerometer
        else if (isMoving == TRUE){
//            if (self.characterState == kStateCollision){
//                [self changeState:kStateCollision];
//                            return;
//            }

            if (direction==1){
                if (self.characterState!=kStateWalkingUp){
                    [self changeState:kStateWalkingUp];
                }
            }
            else if (direction==2){
                if (self.characterState!=kStateWalkingDown){
                    [self changeState:kStateWalkingDown];
                }
                
            }
            else if (direction==3){
                if (self.characterState!=kStateWalkingRight){
                    [self changeState:kStateWalkingRight];
                }
                
            }
            else if (direction==4){
                if (self.characterState!=kStateWalkingLeft){
                    [self changeState:kStateWalkingLeft];
                }
            }
        }
        
        else if (joystick.velocity.x != 0.0f) /*&& (joystick.velocity.y != 0.0f))*/ {
//            if (self.characterState == kStateCollision){
//                [self changeState:kStateCollision];
//                //            return;
//            }

            if (joystick.velocity.x > 0.45f) { 
                if (self.characterState != kStateWalkingRight){
                    [self changeState:kStateWalkingRight];
                    right = TRUE;
                    left = FALSE;
                    up = FALSE;
                    down = FALSE;
                }
                [self applyJoystick:joystick forTimeDelta:deltaTime];
            } 
            else if (joystick.velocity.x < -0.45f) { 
                if (self.characterState != kStateWalkingLeft){
                    [self changeState:kStateWalkingLeft];
                    right = FALSE;
                    left = TRUE;
                    up = FALSE;
                    down = FALSE;
                }
                [self applyJoystick:joystick forTimeDelta:deltaTime];
            } 
            else if (joystick.velocity.y !=0.0f){
                if (joystick.velocity.y > 0.45f) { 
                    if (self.characterState != kStateWalkingUp){
                        [self changeState:kStateWalkingUp];
                        right = FALSE;
                        left = FALSE;
                        up = TRUE;
                        down = FALSE;
                    }
                    [self applyJoystick:joystick forTimeDelta:deltaTime];
                    
                } 
                else if (joystick.velocity.y < 0.45f) { 
                    if (self.characterState != kStateWalkingDown){
                        [self changeState:kStateWalkingDown];
                        right = FALSE;
                        left = FALSE;
                        up = FALSE;
                        down = TRUE;
                    }
                    [self applyJoystick:joystick forTimeDelta:deltaTime];
                    
                } 
                
            }

        }

    } 
    if ([self numberOfRunningActions] == 0) {
        [self changeState:kStateIdle];
    }

}


-(void)initAnimations {
    [self setWalkDownAnim:[self loadPlistForAnimationWithName:@"walkAnimDown" andClassName:NSStringFromClass([self class])]];

    [self setWalkUpAnim:[self loadPlistForAnimationWithName:@"walkAnimUp" andClassName:NSStringFromClass([self class])]];
    
    [self setWalkLeftAnim:[self loadPlistForAnimationWithName:@"walkAnimLeft" andClassName:NSStringFromClass([self class])]];

    [self setWalkRightAnim:[self loadPlistForAnimationWithName:@"walkAnimRight" andClassName:NSStringFromClass([self class])]];

}





- (id)init{
    if( (self=[super init]) ) {
        joystick = nil;
        attackButton = nil;
        attackButton1 = nil;
        self.gameObjectType = kPlayerType;
        idleTime = 0.0f;
        [self initAnimations];        
    }
    return self;
}


@end
